<template>
  <div class="login">
  <el-form :rules="rules" ref="loginForm" class="login-container"
           element-loading-text="正在登录..."
           element-loading-spinner="el-icon-loading"
           element-loading-background="rgba(0, 0, 0, 0.8)"
           :model="loginForm"
           label-position="left"
           label-width="0px" v-loading="loading">
    <h3 class="login_title">企业人才管理系统</h3>
    <el-form-item prop="username">
      <el-input size="normal" type="text" v-model="loginForm.username"
                auto-complete="off" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input size="normal" type="password" v-model="loginForm.password"
                auto-complete="off" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item prop="code">
      <el-input size="normal" type="text" v-model="loginForm.code" auto-complete="off"
                placeholder="点击图片更换验证码" @keydown.enter.native="submitLogin" style="width: 200px;"></el-input>
      <img :src="vcUrl" @click="updateVerifyCode" alt="" style="cursor: pointer;align-content: center;margin-left: 25px;margin-bottom: -10px;">
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>
<script>
export default{
    data(){
      return {
        rules: {
          account: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          checkPass: [{required: true, message: '请输入密码', trigger: 'blur'}],
          code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
        },
        checked: true,
        loginForm: {
          username: '',
          password: '',
          code:''
        },
        vcUrl: '/verifyCode?time='+new Date(),
        loading: false
      }
    },
    methods: {
      updateVerifyCode() {
        this.vcUrl = '/verifyCode?time='+new Date();
      },
      submitLogin: function () {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.postRequest('/login', this.loginForm).then(resp => {
              this.loading = false;
              if (resp && resp.status == 200) {
                var data = resp.data;
                this.$store.commit('login', data.obj);
                var path = this.$route.query.redirect;
                this.$router.replace({path: path == '/' || path == undefined ? '/home' : path});
              }else{
                this.vcUrl = '/verifyCode?time='+new Date();
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>
<style>
  .login-container {
    border-radius: 10px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    /*box-shadow: 0 0 25px #cac6c6;*/
  }
  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .el-form-item_content{
    display: flex;
    align-items: center;
  }
  .login{
    background-image: url('https://masteryhh.oss-cn-beijing.aliyuncs.com/blog_img/202204300945378.png');
    background-size: 100% 100%;
    width: 100%;
    height: 100%;
    position: fixed;
  }

</style>
